package com.xff.Controller;

import com.xff.bean.AdminBean;
import com.xff.bean.HistoryBean;
import com.xff.bean.HistoryPenaltyBean;
import com.xff.dao.AdminDao;
import com.xff.dao.BookDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class IndexController {

    @PostMapping("/LoginServlet")
    public String LoginServlet(HttpServletRequest request, HttpSession session) throws ParseException {
        AdminDao userdao = new AdminDao();
        AdminDao admindao = new AdminDao();
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //对账号和密码进行判断
        boolean result = userdao.Login_verify(username, password);
        //判断输入正确
        if(result){
            AdminBean adminbean = new AdminBean();
            //查询账号和密码查找出读者的信息
            adminbean = admindao.getAdminInfo(username,password);
            //将aid存入session中
            session.setAttribute("aid", ""+adminbean.getAid());
            session.setAttribute("status", adminbean.getStatus());
            //设置session的失效时间
            session.setMaxInactiveInterval(6000);
            //根据status的值来判断是管理员，还是读者，status=1为读者
            if(adminbean.getStatus()==1){
                return "redirect:/index2";
            }else{
                /*
                * 刚刚超期时修改
                * */
                ArrayList<HistoryBean> historyBeanArrayList=new BookDao().get_HistoryListInfo2(1);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date now=new Date();
                for (HistoryBean bean : historyBeanArrayList) {
                    Date endtime = format.parse(bean.getEndtime());
                    if(endtime.getTime()<now.getTime()){
                        new BookDao().overtime(bean.getHid(),1);
                    }
                }

                /*
                * 超期后超期的修改
                * */
                ArrayList<HistoryPenaltyBean> historyPenaltyBeans=new BookDao().get_HistoryPenaltyListInfo2(4);
                for (HistoryPenaltyBean bean : historyPenaltyBeans) {
                    Date endtime = format.parse(bean.getEndtime());
                    long g=24*60*60*1000;
                    int a= (int) ((now.getTime()-endtime.getTime())/g);
                    int times=a/30;/*代表超期times+1次*/
                    if(times>0){
                       if((bean.getHaspaid()+bean.getNopaid())/5!=times+1){
                           int b=times+1-(bean.getHaspaid()+bean.getNopaid())/5;/*还需要交几次超期罚款*/
                           new BookDao().overtime(bean.getHid(),b);
                       }

                    }
                }

                return "redirect:/admin";
            }
        }else{
            //没有找到对应的账号和密码，返回重新登录
            session.setAttribute("state", "密码错误");
            return "redirect:/login";
        }
    }

    @PostMapping("/RegisterServlet")
    public String LoginServlet(HttpServletRequest request){
        //获取注册信息
        AdminDao userdao = new AdminDao();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String sex = request.getParameter("sex");
        String hobby = request.getParameter("hobby");

        //默认设置最大借阅数和借阅天数
        int lend_num = 30;
        int max_num = 5;
        int borrowing_num = 0;


        //将注册信息存入数据库，再返回登录
        userdao.Register(username,password,name,email,phone,lend_num,max_num,sex,hobby,borrowing_num);
        return "redirect:/login";
    }

    @ResponseBody
    @GetMapping("/UsernameVaildate")
    public String UsernameVaildate(HttpServletRequest request){
        String username=request.getParameter("username");
        if(new AdminDao().UsernameVaildate(username)){
            return "ok";
        }
        else
            return "fail";
    }

    @ResponseBody
    @GetMapping("/LoginVaildate")
    public String LoginVaildate(HttpServletRequest request) {
        AdminDao userdao = new AdminDao();
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //对账号和密码进行判断
        boolean result = userdao.Login_verify(username, password);
        //判断输入正确
        if (result) {
            return "ok";
        } else {
            //没有找到对应的账号和密码
            return "fail";
        }
    }

    @ResponseBody
    @GetMapping("/getUser")
    public AdminBean getUser(HttpServletRequest request) {
        AdminDao userdao = new AdminDao();
        String username = request.getParameter("username");

       return userdao.get_AidInfo3(username);
    }
}
